package xx.services.page.rest;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.page.PageNavigationDemo;
import xx.services.Passport;
import xx.services.page.inter.INavigationDemoInterface;
import xx.services.util.Constants;
import xx.util.Arr;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * page页面导航模板管理服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/page/navigation/demo")
 * @author Administrator 2010-12-22
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/page/navigation/demo")
public class NavigationDemoRest extends Passport implements INavigationDemoInterface
{

    public NavigationDemoRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 新增一个导航模板。<br>
     * 注：<br>
     * <li>新增页面导航模板的key，也就是ID是负增长的</li>
     * <li>新增成功后要在page的navigation表中用新增的id做为parent_id新增一条根目录 </li>
     * 
     * @POST
     * @param navigation-@FormParam("navigation")
     *            导航的demo json数据
     * @return 新导航模板
     * @author chenxiang 2010-12-21
     */
    @POST
    public String add(@FormParam("navigation")
    String navigation)
    {
        try
        {
            PageNavigationDemo nd = new PageNavigationDemo(qr, owner);
            JsonParser parser = new JsonParser();
            JsonObject jo = parser.parse(navigation).getAsJsonObject();
            nd.instance(jo);
            nd.insert();
            return ok(nd);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 删除导航模板，同时要删除与之关联的navigaion
     * 
     * @DELETE
     * @Path ("/{navigation_id}")
     * @param navigationId-@PathParam("navigation_id")
     *            模板导航ID，多个ID用“_”隔开
     * @return true-删除成功，string-其他删除失败（string就是失败原因）
     * @author chenxiang 2010-12-21
     */
    @DELETE
    @Path("/{navigation_id}")
    public String delete(@PathParam("navigation_id")
    String navigationId)
    {
        try
        {
            int count = 0;
            long[] navIdArr = Arr.toLongArray(navigationId, "_");
            for (long id : navIdArr)
            {
                PageNavigationDemo nd = new PageNavigationDemo(qr, owner);
                nd.setNavigationId(id).delete();
                count++;
            }
            return ok(count);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 获取一个导航模板，包含与其关联的navigation数据
     * 
     * @GET
     * @Path ("/{navigation_id}")
     * @param navigationId-@PathParam("navigation_id")
     *            模板导航ID
     * @return 导航模板实体JSON数据
     * @author chenxiang 2010-12-21
     */
    @GET
    @Path("/{navigation_id}")
    public String get(@PathParam("navigation_id")
    long navigationId)
    {
        try
        {
            PageNavigationDemo nd = new PageNavigationDemo(qr, owner);
            nd.setNavigationId(navigationId).load();
            return ok(nd);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 获取一个导航模板的HTML代码，包含与其关联的navigation数据
     * 
     * @GET
     * @Path ("/navigation_id/html")
     * @param navigationId-@PathParam("navigation_id")
     *            模板导航ID
     * @return 导航模板HTML代码
     * @author chenxiang 2010-12-21
     */
    @GET
    @Path("/{navigation_id}/html")
    public String getHtml(@PathParam("navigation_id")
    long navigationId)
    {
        try
        {
            PageNavigationDemo nd = new PageNavigationDemo(qr, owner);
            String html = nd.setNavigationId(navigationId).toHtml();
            return ok(html, HTML);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 获取导航模板列表，包含与其关联的navigation数据
     * 
     * @GET
     * @Path ("/list")
     * 
     * @return 导航模板实体JSON数据
     * @author chenxiang 2010-12-21
     */
    @GET
    @Path("/list")
    public String gets()
    {
        try
        {
            PageNavigationDemo pn = new PageNavigationDemo(qr, owner);
            RowSet rs = pn.getNavigationDemoRowSet();
            return ok(pn.rsToList(rs));
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 修改一个导航模板。<br>
     * 
     * @PUT
     * @Path ("/navigation_id")
     * @param navigationId-@PathParam("navigation_id")
     *            模板导航ID
     * @param navigation-@FormParam("navigation")
     *            导航的demo json数据
     * @return 新导航模板
     * @author chenxiang 2010-12-21
     */
    @PUT
    @Path("/{navigation_id}")
    public String update(@PathParam("navigation_id")
    long navigationId, @FormParam("navigation")
    String navigation)
    {
        try
        {
            PageNavigationDemo nd = new PageNavigationDemo(qr, owner);
            nd.setNavigationId(navigationId);
            JsonParser parser = new JsonParser();
            JsonObject jo = parser.parse(navigation).getAsJsonObject();
            nd.update(jo);
            return ok(nd);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }
}
